﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace SpectrEngine.Framework.Collision
{
    public sealed class QuadTree
    {
        private QuadTreeNode headNode;

        private BoundingRect worldBounds;
        public BoundingRect WorldBounds
        {
            get { return worldBounds; }
        }

        private int maxNodes;

        public QuadTree(BoundingRect worldBounds, int maxNodes)
        {
            this.worldBounds = worldBounds;
            this.maxNodes = maxNodes;
            headNode = new QuadTreeNode(null, worldBounds, maxNodes);
        }

        public void Add(CollidableGameNode node)
        {
            if (worldBounds.Contains(node.Bounds))
            {
                headNode.Add(node);
            }
        }

        public void GetNodes(Vector2 point, ref List<CollidableGameNode> nodesFound)
        {
            if (nodesFound != null)
            {
                headNode.GetNodes(point, ref nodesFound);
            }
        }

        public void GetNodes(BoundingRect otherBounds, ref List<CollidableGameNode> nodesFound)
        {
            if (nodesFound != null)
            {
                headNode.GetNodes(otherBounds, ref nodesFound);
            }
        }

        public void GetAllNodes(ref List<CollidableGameNode> nodesFound)
        {
            if (nodesFound != null)
            {
                headNode.GetAllNodes(ref nodesFound);
            }
        }
    }
}
